﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WeiXin
{
    /// <summary>
    /// 解密微信dat图片  返回图片完整路径
    /// </summary>
    public static class WxImage
    {
        public static string DownImage(string fileName)
        {
            string result = "";
            try
            {
                String FileType = "";

                byte[] data = File.ReadAllBytes(fileName);
                byte[] data2 = new byte[data.Length];
                int key = 0;
                //PNG
                for (int i = 0; i <= 0xFF; i++)
                {
                    int dataA = (byte)(data[0] ^ (byte)i);
                    if ((data[0] ^ (byte)i) == (byte)0x89)
                    {
                        if ((data[1] ^ (byte)i) == (byte)0x50)
                        {
                            if ((data[2] ^ (byte)i) == (byte)0x4E)
                            {
                                if ((data[3] ^ (byte)i) == (byte)0x47)
                                {
                                    FileType = "png";
                                    key = i;
                                    break;
                                }
                            }
                        }
                    }
                }

                if (key == 0)
                {
                    //JPG
                    for (int i = 0; i <= 0xFF; i++)
                    {
                        int dataA = (byte)(data[0] ^ (byte)i);
                        if ((data[0] ^ (byte)i) == (byte)0xFF)
                        {
                            if ((data[1] ^ (byte)i) == (byte)0xD8)
                            {
                                if ((data[2] ^ (byte)i) == (byte)0xFF)
                                {
                                    if ((data[3] ^ (byte)i) == (byte)0xE0 || (data[3] ^ (byte)i) == (byte)0xD9 || (data[3] ^ (byte)i) == (byte)0xE1 || (data[3] ^ (byte)i) == (byte)0xDF)
                                    {
                                        FileType = "jpg";
                                        key = i;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                if (key == 0)
                {
                    LogHelper.WriteLog("解密图片失败." + fileName);
                    if(File.Exists(fileName.Replace("dat", "jpg")))
                    {
                        return fileName.Replace("dat", "jpg");
                    }
                    if (File.Exists(fileName.Replace("dat", "png")))
                    {
                        return fileName.Replace("dat", "png");
                    }
                    return "";
                }
                else
                {
                    //this.textBox2.Text = "0x" + key.ToString("X");
                }


                for (int i = 0; i < data.Length; i++)
                {
                    data2[i] = (byte)(data[i] ^ key);
                }

                string picPath = fileName.Replace("dat", "") + FileType;
                File.WriteAllBytes(picPath, data2);

                ExistPic(picPath);

                return picPath;
            }
            catch (Exception e)
            {
                LogHelper.WriteLog("解密图片失败!"+ fileName, e);
                return "";
            }
        }


        public static void EnsureExistOriginalPic(string dataFilePath, string tinyFilePath, ref string result)
        {
            Thread.Sleep(2000);
            if (!File.Exists(dataFilePath))
            {
                if (File.Exists(tinyFilePath))
                {
                    result = WxImage.DownImage(tinyFilePath);
                }
            }
            else
            {
                result = WxImage.DownImage(dataFilePath);
            }

            if (string.IsNullOrEmpty(result))
            {
                Thread.Sleep(100);
                EnsureExistOriginalPic(dataFilePath, tinyFilePath, ref result);
            }
        }

        public static void ExistPic(string picPath)
        {
            if (!File.Exists(picPath))
            {
                Thread.Sleep(50);
                ExistPic(picPath);
            }
        }


    }

    public class ImageModel
    {
        public string fileName { get; set; }

        public string filePath { get; set; }

        public string fileSize { get; set; }

        public string imgHeight { get; set; }

        public string imgWidth { get; set; }
    }
}
